package com.pmsystem.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pmsystem.pojo.Village;
import com.pmsystem.service.VillageService;

/**
 * 
* <p>Title: VillageController</p>  
* <p>Description:处理小区信息的控制器 </p>  
* @author 树昌  
* @date 2019年1月18日
 */
@Controller
@RequestMapping("Information/")
public class VillageController {

	@Autowired
	VillageService villageService;
	
	/**
	 * 
	 * <p>Title: saveVillage</p>  
	 * <p>Description:添加保存小区的信息的控制器 </p>  
	 * @author 树昌
	 * @param village
	 * @return 跳转到显示所有小区信息的页面
	 */
	@RequestMapping(value="addvillage" ,method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> saveVillage(Village village) {
		
		Map<String, Object> map = new HashMap<>();
		String msg = null;
		boolean row = villageService.addVillage(village);
		if(row) {
			msg = "success" ;
		}else {
			msg = "error";
		}
		map.put("msg", msg);
		return map;
	}
	
	
	/**
	 * 
	 * <p>Title: showAllvillage</p>  
	 * <p>Description:显示所有小区的控制器 </p>  
	 * @author 树昌
	 * @return 不做跳转
	 */
	@RequestMapping(value="showallvillage" ,method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> showAllvillage(String keyword, Integer startRow, Integer pageSize) {

		Map<String, Object> map = new HashMap<>();
		List<Village> listvillage = villageService.showVillage(keyword,startRow,pageSize);
		map.put("listvillage", listvillage);
		int size =listvillage.size();
		map.put("size", size);
		return map;
	}
	
	
	@RequestMapping(value="showVillageByKey" ,method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> showVillageByKey(int id){
		
		Map<String, Object> map = new HashMap<>();
		Village village = villageService.showOneVillage(id);
		map.put("village", village);
		return map;
	}
	
	
	/**
	 * 
	 * <p>Title: deleteVillage</p>  
	 * <p>Description:删除小区信息的控制器</p>  
	 * @author 树昌
	 * @return 不做跳转
	 */
	@RequestMapping(value="deletevillage" ,method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> deleteVillage(int id) {
		Map<String, Object> map = new HashMap<>();
		String msg = null;
		Village village = new Village();
		village.setVillageId(id);
		boolean row = villageService.deleteVillage(village);
		if(row) {
			msg = "success";
		}else {
			msg = "error";
		}
		map.put("msg", msg);
		return map;
	}
	
	/**
	 * 
	 * <p>Title: deleteAllVillage</p>  
	 * <p>Description:批量删除小区信息 </p>  
	 * @author 树昌
	 * @param id
	 * @return
	 */
	@RequestMapping(value="deleteAllVillage" ,method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> deleteAllVillage(int [] id) {
		Map<String, Object> map = new HashMap<>();
		String msg=null;
		List<Integer> listID = new ArrayList<>();
		for (int i : id) {
			listID.add(i);
		}
		
		boolean row = villageService.deleteAllVillage(listID);
		
		if(row) {
			msg = "success";
		}else {
			msg = "error";
		}
		map.put("msg", msg);
		return map;
	}
	
	/**
	 * 
	 * <p>Title: updateVillage</p>  
	 * <p>Description:修改一条小区的信息的控制器 </p>  
	 * @author 树昌
	 * @param village
	 * @return 跳转到显示所有小区信息的页面
	 */
	@RequestMapping(value="updatevillage" ,method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> updateVillage(Village village) {
		
		Map<String, Object> map = new HashMap<>();
		String msg=null;
		boolean row = villageService.updateVillage(village);
		if(row) {
			msg = "success";
		}else {
			msg = "error";
		}
		map.put("msg", msg);
		return map;
	}
	
	
	/**
	 * 
	 * <p>Title: deleteVillageTable</p>  
	 * <p>Description:清空数据库表 </p>  
	 * @author 树昌
	 */
	@RequestMapping(value="deleteTable",method=RequestMethod.GET)
	public void deleteVillageTable() {
		villageService.deleteVillageTableData();
	}
	
}
